<template>
	<view class="page">
		<!-- 页面头部内容开始 -->
		<vk-data-page-header
			:title="page.title"
			:show-back="page.showBack"
			@back="pageBack"
		></vk-data-page-header>
		<!-- 页面头部内容结束 -->
		<view class="page-body" v-if="page.complete" v-loading="form1.props.loading">
			<!-- 页面主体内容开始 -->
			<vk-data-form
				ref="form1"
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				:loading.sync="form1.props.loading"
				:labelWidth="form1.props.labelWidth"
				:footer-show="false"
				size="small"
				:success-msg="form1.props.successMsg"
				:before-action="form1.props.beforeAction"
				@success="formSuccess"
			>
				<template v-slot:publish_rule="{ form, keyName, column }">
					<view style="color: #606266;font-size: 14px;">
						<view style="display: flex;">
							<text>单日第一次发布获取</text>
							<vk-data-input-number v-model="form.publish_rule.a" :precision="2" :max="10000000" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
							<text>积分，当日之后每次发布获取</text>
							<vk-data-input-number v-model="form.publish_rule.b" :precision="2" :max="10000000" width="100px" placeholder="多少" style="margin: 0 10px;" size="small"></vk-data-input-number>
							<text>积分</text>
						</view>
						<vk-data-tips :tips="column.tips"></vk-data-tips>
					</view>
				</template>
			</vk-data-form>
			<!-- 页面主体内容结束 -->
		</view>
		<view class="page-footer">
			<!-- 页面底部内容开始 -->
			<el-button v-if="page.showBack" plain style="margin-right: 20px;width: 80px;" @click="pageBack" :loading="form1.props.loading">{{ page.cancelText }}</el-button>
			<el-button type="primary" style="width: 80px;" @click="submitForm" :loading="form1.props.loading">{{ page.submitText }}</el-button>
			<!-- 页面底部内容开始 -->
		</view>
		<view class="page-dialog">
			<!-- 页面弹窗内容开始 -->

			<!-- 页面弹窗内容开始 -->
		</view>
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	var settingKey = "ugc_mission_rule";
	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				data:{

				},
				page: {
					title: "UGC积分规则",
					submitText: "设置",
					cancelText: "返回",
					showBack: false,
					backPage:"",
					complete: false,
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {
						publish_rule:{
							a: 0,
							b: 0
						},
						comment_rule:{
							a: 0,
							b: 0
						},
						share_rule:{
							a: 0,
							b: 0
						},
						like_rule:{
							a: 0,
							b: 0
						},
						collection_rule:{
							a: 0,
							b: 0
						},
						sign_rule: {
							a: 0,
							b: 0
						},
						video_rule: {
							a: 0,
							b: 0
						},
						open_mission: true
					},
					// 表单属性
					props: {
						// 表单请求地址
						action: "admin/sys/ugcMission.updateSetting",
						// 表单字段显示规则
						columns: [
							{ key:"", title:"是否开启任务", type:"bar-title" },
							{ key: "open_mission", title: "", type: "switch", activeValue: true, inactiveValue: false },
							{ key: "", title: "发布积分规则", type: "bar-title" },
							{ key: "publish_rule", title: "", type: "text",  tips:"" },
							{ key: "", title: "评论积分规则", type: "bar-title" },
							{
								key: "comment_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"当日第一次评论获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"当日之后评论获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							{ key:"", title:"分享积分规则", type:"bar-title" },
							{
								key: "share_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"当日第一次分享获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"当日之后分享获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							{ key:"", title:"点赞积分规则", type:"bar-title" },
							{
								key: "like_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"当日第一次点赞获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"当日之后点赞获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							{ key:"", title:"收藏积分规则", type:"bar-title" },
							{
								key: "collection_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"当日第一次收藏获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"当日之后收藏获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							{ key:"", title:"签到积分规则", type:"bar-title" },
							{
								key: "sign_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"第一次签到获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"连续签到7日以上获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							{ key:"", title:"观看奖励视频积分规则", type:"bar-title" },
							{
								key: "video_rule", title: "", type: "object", width:100, showLabel:false,
								columns: [
									{ key:"a" , title:"当日第一次看获取" , type:"number" , width:150, rightText: "积分", precision : 2 },
									{ key:"b" , title:"当日之后以上获取" , type:"number" , width:150, rightText: "积分", precision : 2 }
								]
							},
							
						],
						// 表单验证规则
						rules: {
							publish_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							comment_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							share_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							like_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							collection_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							sign_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							],
							video_rule: [
								// 自定义
								{ validator: (rule, value={}, callback)=>{
									let { a, b } = value;
									let nullKey = vk.pubfn.isNullOneByObject({ a, b });
									if (nullKey) callback(new Error('请完善抵扣规则'));
									if ( a<= 0 || b <= 0) callback(new Error('规则填写错误：数值必须都大于0'));
									callback();
								}, trigger: ['blur','change'] }
							]
						},
						labelWidth: "150px",
						loading:false,
						successMsg:"设置成功",
						beforeAction:(formData)=>{
							// 可在此处修改 formData 后返回 formData，若在此处return false，则表单不触发提交请求。
							return {
								[settingKey]:formData
							};
						},
					}
				}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){

		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				that.$nextTick(() => {
					let mchInfo = vk.getVuex('$user.mchInfo');
					that.initData(mchInfo);
					that.page.complete = true;
					vk.callFunction({
						url: 'client/pub/getMchInfo',
						loading:{ that, name:"form1.props.loading"},
						data:{},
						success:(data) => {
							that.initData(data.mchInfo);
							vk.setVuex('$user.mchInfo',data.mchInfo);
						}
					});
				});
			},
			initData(mchInfo){
				vk.pubfn.objectAssign(this.form1.data, mchInfo[settingKey]);
			},
			// 页面跳转
			pageTo(path){
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(that.page.backPage) {
					vk.navigateTo(that.page.backPage);
				}
			},
			// 表单重置
			resetForm() {
				that.$refs.form1.resetForm();
			},
			// 表单提交
			submitForm(){
				that.$refs.form1.submitForm();
			},
			// 表单提交成功后的回调
			formSuccess(res){
				vk.setVuex("$user.mchInfo", res.data.mchInfo);
			}
		},
		// 监听器
		watch:{

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
